﻿@{
    ViewBag.Title = ViewBag.CurrentMenuName;
    //Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";

    var lstNhomDonVi = ViewBag.lstNhomDonVi;
    var lstDonVi = ViewBag.lstDonVi;

    SelectList slState = new SelectList(new[] {
        new SelectListItem { Value = "2", Text = "Tất cả"},
        new SelectListItem { Value = "0", Text = "Đang sử dụng"},
        new SelectListItem { Value = "1", Text = "Không sử dụng"}
    }, "Value", "Text", 2);
}
<div class="content">
    <div class="clear"></div>
    <table style="width: 100%">
        <tr>
            <td style="height: 40px;">
                <div id="nav"><a href="/Users/Index" title="">@ViewBag.Title.ToUpper()</a></div>
            </td>
        </tr>
        <tr>
            <td style="">
                <div style="width: 95%;">
                    <div class="col-md-2 col-sm-2" style="">@Html.Label("Nhóm đơn vị:")</div>
                    <div class="col-md-4 col-sm-4" style="margin-bottom: 10px;">
                        @Html.DropDownList("nhom_don_vi", lstNhomDonVi as List<SelectListItem>, null, new { @class = "form-control input-sm" })
                    </div>

                    <div class="col-md-2 col-sm-2" style="">@Html.Label("Đơn vị:")</div>
                    <div class="col-md-4 col-sm-4" style="margin-bottom: 10px;">
                        @Html.DropDownList("don_vi", lstDonVi as List<SelectListItem>, null, new { @class = "form-control input-sm" })
                    </div>

                    <div class="col-md-2 col-sm-2" style="">@Html.Label("Họ tên: ", new { id = "lblName", @class = "" })</div>
                    <div class="col-md-4 col-sm-4" style="margin-bottom: 10px;">@Html.TextBox("ho_ten", "", new { @class = "form-control input-sm", style = "", placeholder = "Họ tên", maxlength = 250 })</div>

                    <div class="col-md-2 col-sm-2" style="">@Html.Label("Tên đăng nhập: ")</div>
                    <div class="col-md-4 col-sm-4" style="margin-bottom: 10px;">@Html.TextBox("tai_khoan", "", new { @class = "form-control input-sm", style = "", placeholder = "Tài khoản", maxlength = 50 })</div>

                    @*<div class="col-md-2 col-sm-2" style="">@Html.Label("Điện thoại: ")</div>
                    <div class="col-md-4 col-sm-4" style="margin-bottom: 10px;">@Html.TextBox("dien_thoai", "", new { @class = "form-control input-sm", style = "", placeholder = "Điện thoại", maxlength = 50 })</div>*@

                    <div class="col-md-2 col-sm-2" style="">@Html.Label("Trạng thái")</div>
                        <div class="col-md-4 col-sm-4" style="margin-bottom: 10px;">
                        @Html.DropDownList("trang_thai", slState , null, new { @class = "form-control input-sm" })</div>

                    <div class="col-md-6 col-sm-6">
                        <div class="pull-right">
                            <button id="btnSearch" class="btn btn-primary" type="button"><span class=" glyphicon glyphicon-search"></span> Tìm kiếm</button>
                            <button id="btnAdd" class="btn btn-primary" style="cursor:pointer;"><span class="glyphicon glyphicon-plus"></span> Tạo mới</button>
                            <div id="divPrintEnum" style="display:none;"></div>
                        </div>
                    </div>


                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div id="user-list" style="margin-top:10px;">
                    @if (Model.Data != null && Model.Data.Count > 0)
                    {
                        Html.RenderPartial("_NguoiDungAdminList");
                    }
                    else
                    {
                        <h4>Không tìm thấy kết quả nào phù hợp</h4>
                    }
                </div>
            </td>
        </tr>

    </table>
</div>



<script type="text/javascript">
    $(document).ready(function () {

        $('#btnSearch').bind('click', function (event) {
            btnSearchClick();
        });

        $('#btnAdd').bind('click', function (event) {
            functionCreateOrUpdate("", "Thêm mới", "Tạo mới");
        });

        $('#input-state').change(function () {
            OnCriteria(0);
        });

        $("#input-code", "#input-name").on("keypress", function (event) {
            if (event.which == 13) {
                OnCriteria(0);
            }
        });
        headers['VerificationToken'] = '@GetAntiForgeryToken()';
    });

    var headers = {};

    function btnSearchClick() {
        OnCriteria(0);
    }

    function nhomdonviEdit(id) {
        functionCreateOrUpdate(id, "Cập nhật", "Cập nhật");
    }

    $('#nhom_don_vi').change(function () {
        var nhomid = $('#nhom_don_vi').val();
        $.ajax({
            dataType: "json",
            type: "POST",
            url: '/NguoiDungAdmin/GetDonViByNhom',
            contentType: "application/json",
            data: JSON.stringify({ strNhomDonViID: nhomid }),
            success: function (data) {
                var result = '';
                for (var i = 0; i < data.length; i++) {
                    result += '<option value="' + data[i].Value + '">' + data[i].Text + '</option>';
                }
                $("#don_vi").html(result);
                //OnCriteria(0);
            }
        });
    });

    function functionCreateOrUpdate(id, titleForm, buttonTile) {
        $("#divLoading").show();
        var ajax = $.ajax({
            url: '/NguoiDungAdmin/CreatePopupUpdate',
            type: 'POST',
            data: { id: id },
            //contentType: "appliction/json",
            success: function (data) {
                $("#divLoading").hide();
                var box = bootbox.dialog({
                    title: titleForm,
                    message: data,
                    onEscape: function () {
                        box.modal('hide');
                    },
                    buttons: {
                        success: {
                            label: "<i class='fa fa-plus-square-o'></i> " + buttonTile,
                            //className: "btn btn-md btn-success",
                            className: "btn btn-primary",
                            callback: function () {
                                var form = $('#frmNguoiDungAdmin');
                                if (!form.valid()) {
                                    return false;
                                }
                                $("#divLoading").show();
                                //alert("headers");
                                $.ajax({
                                    url: '/NguoiDungAdmin/ActionSave',
                                    headers: headers,
                                    data: {
                                        NGUOI_DUNG_ID: $("#NGUOI_DUNG_ID").val(),
                                        DON_VI_ID: $("#DON_VI_ID").val(),
                                        HO_VA_TEN: $("#HO_VA_TEN").val(),
                                        TAI_KHOAN: $("#TAI_KHOAN").val(),
                                        DIEN_THOAI: $("#DIEN_THOAI").val()
                                    },
                                    dataType: 'json',
                                    type: 'post',
                                    success: function (response) {
                                        if (response != null && response.status == 200) {
                                            //Layout.toastr("", response.result);
                                            window.notice(response.result, window.notice_success);
                                            OnCriteria(currentPageIndex);
                                            result = true;
                                        } else {
                                            //Layout.toastr("", response.result);
                                            window.notice(response.result, window.notice_error);
                                        }
                                        $("#divLoading").hide();
                                    },
                                    error: function (errorThrown) {//function(request, textStatus, errorThrown)  {
                                        alert(JSON.stringify(errorThrown));
                                        //alert(request.getAllResponseHeaders());
                                        //alert(request.getResponseHeader('WWW-Authenticate'));
                                    }
                                });

                            }
                        },
                        danger: {
                            label: "<i class='fa fa-reply'></i> Hủy bỏ",
                            className: "btn btn-md btn-default",//"btn-danger",
                            callback: function () {
                            }
                        }
                    }
                });
            },
            complete: function () {
                setTimeout(function () {
                    $("#HO_VA_TEN").focus();
                }, 500);
            },
            error: function (ex) {
                alert(JSON.stringify(ex));
            }
        });
        return false;
    }

    var currentPageIndex;
    function OnCriteria(pageIndex) {
        currentPageIndex = pageIndex;
        var name = $('#ho_ten').val().trim();
        var account = $("#tai_khoan").val().trim();
        //var phone = $("#dien_thoai").val().trim();
        var status = $("#trang_thai").val().trim();
        var nhomdonvi = $('#nhom_don_vi').val().trim();
        var donvi = $('#don_vi').val().trim();
        $.ajax({
            dataType: "html",
            type: "POST",
            url: '/NguoiDungAdmin/ActionSearch',
            //contentType: "application/json",
            data: { NHOM_DON_VI_ID: nhomdonvi, DON_VI_ID: donvi, HO_VA_TEN: name, TAI_KHOAN: account, DELETED: status, pageIndex: pageIndex },
            success: function (response) {
                //var $catlist = $('#gridContent');
                var $catlist = $('#user-list');

                if ($catlist == null || $catlist.length == 0) {
                    window.notice('@*@Resources.Localization.MsgItemNotFound*@', window.notice_error);
                }
                else {
                    $catlist.html(response);
                }
            }
        });
        setTimeout(function () { $('#search_form').find('input:text:visible:first').focus(); }, 500);
    }

    @functions{
        public string GetAntiForgeryToken()
        {
            string cookieToken, formToken;
            AntiForgery.GetTokens(null, out cookieToken, out formToken);
            return cookieToken + "," + formToken;
        }
    }
</script>
